package org.lvslb.db.dao;


import guest.Guest;
import org.lvslb.db.pojo.AccessLog;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

/**
 * Created by MelOnDev1 on 13/05/14.
 */
@Component
public class AccessDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void persist(AccessLog log) {
        em.persist(log);
    }

    @Transactional
    public void deleteAll() {
        TypedQuery<AccessLog> query = em.createQuery(
                "SELECT g FROM AccessLog g ORDER BY g.id", AccessLog.class);
        for(AccessLog log : query.getResultList()){
            em.remove(log);
        }
    }

    // Retrieves all the guests:
    public List<AccessLog> getAllAccessLog() {
        TypedQuery<AccessLog> query = em.createQuery(
                "SELECT g FROM AccessLog g ORDER BY g.id", AccessLog.class);
        return query.getResultList();
    }
}
